package ca.usask.cs.Gui;

import java.awt.Color;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import javax.naming.CannotProceedException;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableModel;
import  org.jdesktop.layout.GroupLayout;

import ca.usask.cs.drawTree.DrawControler;
import ca.usask.cs.exceptions.GuiException;

/*
 * A class that build tree by interface,
 *  
 * @author Wenyi An
 * August,2010
 */

//	public class CreateNewTreeGuiidea extends javax.swing.JFrame {

public class CreateNewTreeGui extends javax.swing.JFrame {
	private LoadVensimGui loadVensimUI;
	private String[] vensimVarNames;
	private String vensimName;
	private InterfaceHandler interfaceHandler;
	DrawControler drawControler;

	/** Creates new form NewJFrame2 */
	public CreateNewTreeGui(LoadVensimGui loadVensimUI,
			InterfaceHandler interfaceHandler,DrawControler drawControler) {
		this.interfaceHandler = interfaceHandler;
		this.loadVensimUI = loadVensimUI;
		this.drawControler = drawControler;
		vensimVarNames = this.interfaceHandler.getVensimVarNamesbyArray();
		this.vensimName = loadVensimUI.vensimLocation.getText().trim();
		initComponents();
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	private void initComponents() {

		jPanel1 = new JPanel();
		jLabel2 = new JLabel();
		jLabel3 = new JLabel();
		jLabel4 = new JLabel();
		NameTextbox = new JTextField();
		layersTextbox = new JTextField();
		startTimeTextbox = new JTextField();
		treeNameErrMsg = new JLabel();
		layerErrMsg = new JLabel();
		startTimeErrMsg = new JLabel();
		jPanel2 = new JPanel();
		jLabel8 = new JLabel();
		dNodeVensimVarNamesCombobox = new JComboBox();
		jLabel9 = new JLabel();
		dNodeTimeTextbox = new JTextField();
		jScrollPane1 = new JScrollPane();
		dEdgesTable = new JTable();
		jButton1 = new JButton();
		jButton2 = new JButton();
		jLabel7 = new JLabel();
		dBranchTimeErrMsg = new JLabel();
		dTableErrMsg = new JLabel();
		jPanel3 = new JPanel();
		jLabel6 = new JLabel();
		tNodeVensimVarNamesCombo = new JComboBox();
		jPanel5 = new JPanel();
		jLabel11 = new JLabel();
		eNodeVensimVarNamesCombobox = new JComboBox();
		jLabel12 = new JLabel();
		elayerTimeTextbox = new JTextField();
		jScrollPane2 = new JScrollPane();
		eEdgesTable = new JTable();
		jLabel15 = new JLabel();
		jButton3 = new JButton();
		jButton4 = new JButton();
		eBranchTimeErrMsg = new JLabel();
		eTableTimeErrMsg = new JLabel();
		jButton7 = new JButton();
		runButton = new JButton();
		backButton = new JButton();
		jPanel4 = new JPanel();
		dNodeRadioButton = new JRadioButton();
		eNodeRadioButton = new JRadioButton();
		buttonGroup = new ButtonGroup();
		errorSignal = false;

		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		setTitle("Decision Tree");

		jPanel1.setBorder(javax.swing.BorderFactory
				.createTitledBorder("Tree structure"));

		jLabel2.setText("Name");

		jLabel3.setText("Count of Decision plus Event Node Layers");

		jLabel4.setText("Occurrence Time of First (Decision or Event) Node");

		NameTextbox.setText("Example Tree Name");

		layersTextbox.setText("1");
		layersTextbox.getDocument().addDocumentListener(new LayerListener());

		startTimeTextbox.setText("0.0");

		treeNameErrMsg.setText("");
		treeNameErrMsg.setForeground(new Color(255, 0, 0));
		layerErrMsg.setText("");
		layerErrMsg.setForeground(new Color(255, 0, 0));
		startTimeErrMsg.setText("");
		startTimeErrMsg.setForeground(new Color(255, 0, 0));
		org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(
				jPanel1);
		jPanel1.setLayout(jPanel1Layout);
		jPanel1Layout
				.setHorizontalGroup(jPanel1Layout
						.createParallelGroup(
								org.jdesktop.layout.GroupLayout.LEADING)
						.add(
								jPanel1Layout
										.createSequentialGroup()
										.add(53, 53, 53)
										.add(
												jPanel1Layout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.LEADING)
														.add(jLabel3)
														.add(jLabel2)
														.add(jLabel4))
										.add(47, 47, 47)
										.add(
												jPanel1Layout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.LEADING,
																false)
														.add(
																startTimeTextbox)
														.add(
																layersTextbox)
														.add(
																NameTextbox,
																org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																142,
																Short.MAX_VALUE))
										.addPreferredGap(
												org.jdesktop.layout.LayoutStyle.RELATED)
										.add(
												jPanel1Layout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.LEADING)
														.add(
																jPanel1Layout
																		.createSequentialGroup()
																		.add(
																				startTimeErrMsg,
																				org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																				123,
																				Short.MAX_VALUE)
																		.addContainerGap())
														.add(
																jPanel1Layout
																		.createSequentialGroup()
																		.add(
																				layerErrMsg,
																				org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																				123,
																				Short.MAX_VALUE)
																		.addContainerGap())
														.add(
																treeNameErrMsg,
																org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																133,
																Short.MAX_VALUE))));
		jPanel1Layout
				.setVerticalGroup(jPanel1Layout
						.createParallelGroup(
								org.jdesktop.layout.GroupLayout.LEADING)
						.add(
								jPanel1Layout
										.createSequentialGroup()
										.addContainerGap()
										.add(
												jPanel1Layout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.BASELINE)
														.add(jLabel2)
														.add(
																NameTextbox,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
														.add(
																treeNameErrMsg))
										.add(20, 20, 20)
										.add(
												jPanel1Layout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.BASELINE)
														.add(jLabel3)
														.add(
																layersTextbox,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
														.add(
																layerErrMsg))
										.add(23, 23, 23)
										.add(
												jPanel1Layout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.BASELINE)
														.add(
																jLabel4,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																12,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
														.add(
																startTimeTextbox,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
														.add(
																startTimeErrMsg))
										.addContainerGap(29, Short.MAX_VALUE)));

		jPanel2.setBorder(javax.swing.BorderFactory
				.createTitledBorder("Decision Node Branches"));

		jLabel8.setText("Variable");

		dNodeVensimVarNamesCombobox
				.setModel(new javax.swing.DefaultComboBoxModel(vensimVarNames));

		jLabel9.setText("Time until following layer ");
		dNodeTimeTextbox.setColumns(8);
		dNodeTimeTextbox.setText("1.0");

		dEdgesTable.setModel(new javax.swing.table.DefaultTableModel(
				new Object[][] { { null, null }, { null, null },
						{ null, null }, { null, null }, { null, null },
						{ null, null } }, new String[] { "Name", "Value" }));
		jScrollPane1.setViewportView(dEdgesTable);

		jButton1.setText("Add");
		jButton1.addActionListener(new AddRow(dEdgesTable));
		jButton2.setText("Delete");
		jButton2.addActionListener(new DeleteRow(dEdgesTable));
		jLabel7.setText("Decision Nodes Branches");

		dBranchTimeErrMsg.setText("");
		dBranchTimeErrMsg.setForeground(new Color(255, 0, 0));
		dTableErrMsg.setText("");
		dTableErrMsg.setForeground(new Color(255, 0, 0));
		org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(
				jPanel2);
		jPanel2.setLayout(jPanel2Layout);
		jPanel2Layout
				.setHorizontalGroup(jPanel2Layout
						.createParallelGroup(
								org.jdesktop.layout.GroupLayout.LEADING)
						.add(
								jPanel2Layout
										.createSequentialGroup()
										.addContainerGap()
										.add(
												jPanel2Layout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.LEADING,
																false)
														.add(
																jPanel2Layout
																		.createSequentialGroup()
																		.add(
																				jPanel2Layout
																						.createParallelGroup(
																								org.jdesktop.layout.GroupLayout.LEADING)
																						.add(
																								jLabel8)
																						.add(
																								jLabel9))
																		.add(
																				18,
																				18,
																				18)
																		.add(
																				jPanel2Layout
																						.createParallelGroup(
																								org.jdesktop.layout.GroupLayout.LEADING,
																								false)
																						.add(
																								jPanel2Layout
																										.createSequentialGroup()
																										.add(
																												dNodeTimeTextbox,
																												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																												org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
																										.add(
																												18,
																												18,
																												18)
																										.add(
																												dBranchTimeErrMsg,
																												org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																												org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																												Short.MAX_VALUE))
																						.add(
																								dNodeVensimVarNamesCombobox,
																								org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																								282,
																								org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
														.add(
																jPanel2Layout
																		.createSequentialGroup()
																		.add(
																				jLabel7)
																		.add(
																				18,
																				18,
																				18)
																		.add(
																				dTableErrMsg,
																				org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																				org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																				Short.MAX_VALUE))
														.add(
																jPanel2Layout
																		.createSequentialGroup()
																		.add(
																				jScrollPane1,
																				org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																				272,
																				org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
																		.add(
																				50,
																				50,
																				50)
																		.add(
																				jPanel2Layout
																						.createParallelGroup(
																								org.jdesktop.layout.GroupLayout.LEADING,
																								false)
																						.add(
																								jButton1,
																								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																								Short.MAX_VALUE)
																						.add(
																								jButton2,
																								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																								Short.MAX_VALUE))))
										.addContainerGap(30, Short.MAX_VALUE)));
		jPanel2Layout
				.setVerticalGroup(jPanel2Layout
						.createParallelGroup(
								org.jdesktop.layout.GroupLayout.LEADING)
						.add(
								jPanel2Layout
										.createSequentialGroup()
										.addContainerGap()
										.add(
												jPanel2Layout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.BASELINE)
														.add(jLabel8)
														.add(
																dNodeVensimVarNamesCombobox,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												org.jdesktop.layout.LayoutStyle.UNRELATED)
										.add(
												jPanel2Layout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.LEADING)
														.add(
																jPanel2Layout
																		.createSequentialGroup()
																		.add(
																				32,
																				32,
																				32)
																		.add(
																				jPanel2Layout
																						.createParallelGroup(
																								org.jdesktop.layout.GroupLayout.BASELINE)
																						.add(
																								jLabel7)
																						.add(
																								dTableErrMsg)))
														.add(
																jPanel2Layout
																		.createParallelGroup(
																				org.jdesktop.layout.GroupLayout.BASELINE)
																		.add(
																				dNodeTimeTextbox,
																				org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																				20,
																				org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
																		.add(
																				jLabel9)
																		.add(
																				dBranchTimeErrMsg)))
										.add(18, 18, 18)
										.add(
												jPanel2Layout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.TRAILING)
														.add(
																jScrollPane1,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																123,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
														.add(
																jPanel2Layout
																		.createSequentialGroup()
																		.add(
																				jButton1)
																		.addPreferredGap(
																				org.jdesktop.layout.LayoutStyle.UNRELATED)
																		.add(
																				jButton2)))
										.addContainerGap(28, Short.MAX_VALUE)));

		jPanel3.setBorder(javax.swing.BorderFactory
				.createTitledBorder("Terminal Nodes"));

		jLabel6.setText("Variable");

		tNodeVensimVarNamesCombo.setModel(new javax.swing.DefaultComboBoxModel(
				vensimVarNames));

		org.jdesktop.layout.GroupLayout jPanel3Layout = new org.jdesktop.layout.GroupLayout(
				jPanel3);
		jPanel3.setLayout(jPanel3Layout);
		jPanel3Layout
				.setHorizontalGroup(jPanel3Layout
						.createParallelGroup(
								org.jdesktop.layout.GroupLayout.LEADING)
						.add(
								jPanel3Layout
										.createSequentialGroup()
										.add(39, 39, 39)
										.add(jLabel6)
										.addPreferredGap(
												org.jdesktop.layout.LayoutStyle.RELATED,
												42, Short.MAX_VALUE)
										.add(
												tNodeVensimVarNamesCombo,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
												248,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
										.add(36, 36, 36)));
		jPanel3Layout
				.setVerticalGroup(jPanel3Layout
						.createParallelGroup(
								org.jdesktop.layout.GroupLayout.LEADING)
						.add(
								jPanel3Layout
										.createSequentialGroup()
										.add(59, 59, 59)
										.add(
												jPanel3Layout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.BASELINE)
														.add(jLabel6)
														.add(
																tNodeVensimVarNamesCombo,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
										.addContainerGap(67, Short.MAX_VALUE)));

		jPanel5.setBorder(javax.swing.BorderFactory
				.createTitledBorder("Event Nodes"));

		jLabel11.setText("Variable");

		eNodeVensimVarNamesCombobox
				.setModel(new javax.swing.DefaultComboBoxModel(vensimVarNames));

		jLabel12.setText("Time until following layer ");
		elayerTimeTextbox.setColumns(8);
		elayerTimeTextbox.setText("1.0");

		eEdgesTable.setModel(new javax.swing.table.DefaultTableModel(
				new Object[][] { { null, null, null }, { null, null, null },
						{ null, null, null }, { null, null, null },
						{ null, null, null }, { null, null, null } },
				new String[] { "Name", "Value", "Probability" }));

		eEdgesTable.addMouseListener(new ProbListener());

		jScrollPane2.setViewportView(eEdgesTable);

		jLabel15.setText("Event Node Branches");

		jButton3.setText("Add");
		jButton3.addActionListener(new AddRow(eEdgesTable));
		jButton4.setText("Delete");
		jButton4.addActionListener(new DeleteRow(eEdgesTable));

		eBranchTimeErrMsg.setText("");
		eBranchTimeErrMsg.setForeground(new Color(255, 0, 0));
		eTableTimeErrMsg.setText("");
		eTableTimeErrMsg.setForeground(new Color(255, 0, 0));
		jButton7.setText("Auto Fill Remaining Probability");
		jButton7.addActionListener(new AutoFillListener());

		org.jdesktop.layout.GroupLayout jPanel5Layout = new org.jdesktop.layout.GroupLayout(
				jPanel5);
		jPanel5.setLayout(jPanel5Layout);
		jPanel5Layout
				.setHorizontalGroup(jPanel5Layout
						.createParallelGroup(
								org.jdesktop.layout.GroupLayout.LEADING)
						.add(
								jPanel5Layout
										.createSequentialGroup()
										.add(
												jPanel5Layout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.LEADING)
														.add(
																jPanel5Layout
																		.createSequentialGroup()
																		.addContainerGap()
																		.add(
																				jPanel5Layout
																						.createParallelGroup(
																								org.jdesktop.layout.GroupLayout.LEADING)
																						.add(
																								jPanel5Layout
																										.createSequentialGroup()
																										.add(
																												jPanel5Layout
																														.createParallelGroup(
																																org.jdesktop.layout.GroupLayout.LEADING)
																														.add(
																																jLabel11)
																														.add(
																																jLabel12))
																										.add(
																												34,
																												34,
																												34)
																										.add(
																												jPanel5Layout
																														.createParallelGroup(
																																org.jdesktop.layout.GroupLayout.LEADING)
																														.add(
																																eNodeVensimVarNamesCombobox,
																																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																																252,
																																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
																														.add(
																																jPanel5Layout
																																		.createParallelGroup(
																																				org.jdesktop.layout.GroupLayout.TRAILING)
																																		.add(
																																				org.jdesktop.layout.GroupLayout.LEADING,
																																				eTableTimeErrMsg,
																																				org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																																				292,
																																				Short.MAX_VALUE)
																																		.add(
																																				org.jdesktop.layout.GroupLayout.LEADING,
																																				jPanel5Layout
																																						.createSequentialGroup()
																																						.add(
																																								elayerTimeTextbox,
																																								org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																																								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																																								org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
																																						.addPreferredGap(
																																								org.jdesktop.layout.LayoutStyle.UNRELATED)
																																						.add(
																																								eBranchTimeErrMsg,
																																								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																																								223,
																																								Short.MAX_VALUE)))))
																						.add(
																								jPanel5Layout
																										.createSequentialGroup()
																										.add(
																												jScrollPane2,
																												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																												377,
																												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
																										.addPreferredGap(
																												org.jdesktop.layout.LayoutStyle.RELATED)
																										.add(
																												jPanel5Layout
																														.createParallelGroup(
																																org.jdesktop.layout.GroupLayout.LEADING,
																																false)
																														.add(
																																jButton4,
																																org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																																org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																																Short.MAX_VALUE)
																														.add(
																																jButton3,
																																org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																																63,
																																Short.MAX_VALUE)))
																						.add(
																								jLabel15,
																								org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																								123,
																								org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
														.add(
																jPanel5Layout
																		.createSequentialGroup()
																		.add(
																				130,
																				130,
																				130)
																		.add(
																				jButton7)))
										.add(24, 24, 24)));
		jPanel5Layout
				.setVerticalGroup(jPanel5Layout
						.createParallelGroup(
								org.jdesktop.layout.GroupLayout.LEADING)
						.add(
								jPanel5Layout
										.createSequentialGroup()
										.addContainerGap()
										.add(
												jPanel5Layout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.BASELINE)
														.add(
																eNodeVensimVarNamesCombobox,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
														.add(jLabel11))
										.addPreferredGap(
												org.jdesktop.layout.LayoutStyle.UNRELATED)
										.add(
												jPanel5Layout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.BASELINE)
														.add(
																elayerTimeTextbox,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
														.add(jLabel12)
														.add(
																eBranchTimeErrMsg))
										.addPreferredGap(
												org.jdesktop.layout.LayoutStyle.UNRELATED)
										.add(
												jPanel5Layout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.BASELINE)
														.add(jLabel15)
														.add(
																eTableTimeErrMsg))
										.add(18, 18, 18)
										.add(
												jPanel5Layout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.LEADING)
														.add(
																org.jdesktop.layout.GroupLayout.TRAILING,
																jPanel5Layout
																		.createSequentialGroup()
																		.add(
																				jButton3)
																		.addPreferredGap(
																				org.jdesktop.layout.LayoutStyle.UNRELATED)
																		.add(
																				jButton4))
														.add(
																org.jdesktop.layout.GroupLayout.TRAILING,
																jScrollPane2,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																123,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												org.jdesktop.layout.LayoutStyle.RELATED,
												org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE).add(
												jButton7)));

		runButton.setFont(new java.awt.Font("Tahoma", 1, 13)); // NOI18N
		runButton.setText("Run Tree");
		runButton.addActionListener(new CreateTreeListener());
		backButton.setFont(new java.awt.Font("Tahoma", 1, 13)); // NOI18N
		backButton.setText("Back");
		backButton.addActionListener(new BackListener());
		jPanel4.setBorder(javax.swing.BorderFactory
				.createTitledBorder("Starting Node"));

		dNodeRadioButton.setText("Decision Node");
		dNodeRadioButton.setSelected(true);
		eNodeRadioButton.setText("Event Node");
		buttonGroup.add(dNodeRadioButton);
		buttonGroup.add(eNodeRadioButton);
		dNodeRadioButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				interfaceHandler.isStartByENode = false;
				drawColumn();
			}
		});

		eNodeRadioButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				interfaceHandler.isStartByENode = true;
				drawColumn();

			}
		});

		org.jdesktop.layout.GroupLayout jPanel4Layout = new org.jdesktop.layout.GroupLayout(
				jPanel4);
		jPanel4.setLayout(jPanel4Layout);
		jPanel4Layout.setHorizontalGroup(jPanel4Layout.createParallelGroup(
				org.jdesktop.layout.GroupLayout.LEADING).add(
				jPanel4Layout.createSequentialGroup().add(65, 65, 65)
						.add(dNodeRadioButton).add(77, 77, 77)
						.add(eNodeRadioButton).addContainerGap(144,
								Short.MAX_VALUE)));
		jPanel4Layout.setVerticalGroup(jPanel4Layout.createParallelGroup(
				org.jdesktop.layout.GroupLayout.LEADING).add(
				jPanel4Layout.createParallelGroup(
						org.jdesktop.layout.GroupLayout.BASELINE)
						.add(dNodeRadioButton).add(
								eNodeRadioButton)));

		org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout
				.setHorizontalGroup(layout
						.createParallelGroup(
								org.jdesktop.layout.GroupLayout.LEADING)
						.add(
								layout
										.createSequentialGroup()
										.add(
												layout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.LEADING)
														.add(
																layout
																		.createSequentialGroup()
																		.addContainerGap()
																		.add(
																				layout
																						.createParallelGroup(
																								org.jdesktop.layout.GroupLayout.LEADING)
																						.add(
																								jPanel2,
																								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																								Short.MAX_VALUE)
																						.add(
																								org.jdesktop.layout.GroupLayout.TRAILING,
																								jPanel4,
																								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																								Short.MAX_VALUE)
																						.add(
																								org.jdesktop.layout.GroupLayout.TRAILING,
																								jPanel1,
																								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																								Short.MAX_VALUE))
																		.add(
																				48,
																				48,
																				48))
														.add(
																layout
																		.createSequentialGroup()
																		.add(
																				256,
																				256,
																				256)
																		.add(
																				backButton,
																				org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																				92,
																				org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
																		.addPreferredGap(
																				org.jdesktop.layout.LayoutStyle.RELATED)))
										.add(
												layout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.LEADING)
														.add(
																layout
																		.createParallelGroup(
																				org.jdesktop.layout.GroupLayout.LEADING)
																		.add(
																				layout
																						.createSequentialGroup()
																						.add(
																								jPanel3,
																								org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																								org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
																						.add(
																								73,
																								73,
																								73))
																		.add(
																				layout
																						.createSequentialGroup()
																						.add(
																								jPanel5,
																								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																								Short.MAX_VALUE)
																						.addContainerGap()))
														.add(
																layout
																		.createSequentialGroup()
																		.add(
																				172,
																				172,
																				172)
																		.add(
																				runButton,
																				org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																				100,
																				org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
																		.addContainerGap()))));
		layout
				.setVerticalGroup(layout
						.createParallelGroup(
								org.jdesktop.layout.GroupLayout.LEADING)
						.add(
								layout
										.createSequentialGroup()
										.add(48, 48, 48)
										.add(
												layout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.TRAILING)
														.add(
																jPanel1,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
														.add(
																jPanel3,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
										.add(18, 18, 18)
										.add(
												jPanel4,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
												org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												org.jdesktop.layout.LayoutStyle.RELATED,
												16, Short.MAX_VALUE)
										.add(
												layout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.LEADING,
																false)
														.add(
																jPanel2,
																org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																Short.MAX_VALUE)
														.add(
																jPanel5,
																org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																Short.MAX_VALUE))
										.addPreferredGap(
												org.jdesktop.layout.LayoutStyle.RELATED,
												org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)
										.add(
												layout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.BASELINE)
														.add(
																backButton,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																29,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
														.add(
																runButton,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																30,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
										.addContainerGap()));

		pack();
	}// </editor-fold>

	// Variables declaration - do not modify
	private javax.swing.JButton jButton1;
	private javax.swing.JButton jButton2;
	private javax.swing.JButton jButton3;
	private javax.swing.JButton jButton4;
	private javax.swing.JButton backButton;
	private javax.swing.JButton runButton;
	private javax.swing.JButton jButton7;
	private javax.swing.JComboBox tNodeVensimVarNamesCombo;
	private javax.swing.JComboBox dNodeVensimVarNamesCombobox;
	private javax.swing.JComboBox eNodeVensimVarNamesCombobox;
	private javax.swing.JLabel treeNameErrMsg;
	private javax.swing.JLabel startTimeErrMsg;
	private javax.swing.JLabel jLabel11;
	private javax.swing.JLabel jLabel12;
	private javax.swing.JLabel dBranchTimeErrMsg;
	private javax.swing.JLabel eBranchTimeErrMsg;
	private javax.swing.JLabel jLabel15;
	private javax.swing.JLabel dTableErrMsg;
	private javax.swing.JLabel eTableTimeErrMsg;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel jLabel4;
	private javax.swing.JLabel layerErrMsg;
	private javax.swing.JLabel jLabel6;
	private javax.swing.JLabel jLabel7;
	private javax.swing.JLabel jLabel8;
	private javax.swing.JLabel jLabel9;
	private javax.swing.JPanel jPanel1;
	private javax.swing.JPanel jPanel2;
	private javax.swing.JPanel jPanel3;
	private javax.swing.JPanel jPanel4;
	private javax.swing.JPanel jPanel5;
	private ButtonGroup buttonGroup;
	private javax.swing.JRadioButton dNodeRadioButton;
	private javax.swing.JRadioButton eNodeRadioButton;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JScrollPane jScrollPane2;
	private javax.swing.JTable dEdgesTable;
	private javax.swing.JTable eEdgesTable;
	private javax.swing.JTextField NameTextbox;
	private javax.swing.JTextField layersTextbox;
	private javax.swing.JTextField startTimeTextbox;
	private javax.swing.JTextField dNodeTimeTextbox;
	private javax.swing.JTextField elayerTimeTextbox;
	private boolean errorSignal;

	// End of variables declaration

	private class CreateTreeListener implements ActionListener {
		public void actionPerformed(ActionEvent event) {

			clearErrorMsg();
			cancleFocus();

			int numOfLayers = 1;
			double starttime = 0.0;
			int numofProb = 1;
			String treeName = "Example tree";
			double DNodetime = 0.0;
			double CNodetime = 0.0;
			
			try {
				numOfLayers = checkLayers();
				starttime = checkTimes(startTimeTextbox,startTimeErrMsg);
				checkDecisionTable();
				checkEventTable();
				treeName = checkText(NameTextbox);
				DNodetime = checkTimes(dNodeTimeTextbox,dBranchTimeErrMsg);
				CNodetime = checkTimes(elayerTimeTextbox,eBranchTimeErrMsg);
				errorSignal = false;
			} catch (Exception e) {
				errorSignal = true;
				// return;
			} finally {
				if (errorSignal) {
					return;
				}
			}

	//		ca.usask.cs.drawTree.DrawControler.layer = numOfLayers;
			drawControler.layer = numOfLayers;
		
			drawControler.rootx = (float) starttime;
			drawControler.TreeName = treeName;
			drawControler.dBranchTime = (float) DNodetime;
			drawControler.cBranchTime = (float) CNodetime;
			if(interfaceHandler.isStartByENode){
				drawControler.type = 1 ;
				}
			else{
				drawControler.type = 0 ;
			}
			
			drawControler.DecisionLayerNumberofEdges = interfaceHandler.BasicEdgeList.size();
			drawControler.ChanceLayerNumberofEdges = interfaceHandler.ChanceEdgeList.size();
			

			if (interfaceHandler.isStartByENode == false) {
				int treeID = 1;
				String rootNodevarName = (String) dNodeVensimVarNamesCombobox
						.getSelectedItem();
				int chancetNodeID = 2;

				String DNodevarName = (String) (dNodeVensimVarNamesCombobox
						.getSelectedItem());
				interfaceHandler.parseDecisionTree(treeName, treeID, starttime,
						rootNodevarName);

				String CNodevarName = (String) eNodeVensimVarNamesCombobox
						.getSelectedItem();
				String TNodevarName = (String) tNodeVensimVarNamesCombo
						.getSelectedItem();

				interfaceHandler.createTree(numOfLayers, numofProb,
						chancetNodeID, DNodetime, DNodevarName, CNodetime,
						CNodevarName, TNodevarName, starttime, false);
			} else {

				interfaceHandler.parseDecisionTree(treeName, 1, starttime,
						(String) eNodeVensimVarNamesCombobox.getSelectedItem());
				interfaceHandler.createTree(numOfLayers, numofProb, 2,
						DNodetime, (String) (dNodeVensimVarNamesCombobox
								.getSelectedItem()), CNodetime,
						(String) eNodeVensimVarNamesCombobox.getSelectedItem(),
						(String) tNodeVensimVarNamesCombo.getSelectedItem(),
						starttime, true);
			}

			setVisible(false);	
			interfaceHandler.InitVensim(vensimName);
			interfaceHandler.run(/* startTimeTextbox.getText().trim() */);
			interfaceHandler.rollback();
			interfaceHandler.createPictureInterface(false);   // for now, no graphical interface; change to "TRUE" for a graphical interface
		}

	}

	private void clearErrorMsg() {
		treeNameErrMsg.setText("");
		layerErrMsg.setText("");
		startTimeErrMsg.setText("");
		dBranchTimeErrMsg.setText("");
		dTableErrMsg.setText("");
		eBranchTimeErrMsg.setText("");
		eTableTimeErrMsg.setText("");
	}

	private void cancleFocus() {
		if (dEdgesTable.isEditing() && dEdgesTable.getCellEditor() != null) {
			dEdgesTable.getCellEditor().stopCellEditing();
		}
		if (eEdgesTable.isEditing() && eEdgesTable.getCellEditor() != null) {
			eEdgesTable.getCellEditor().stopCellEditing();
		}
	}


	private void eTableCheckprob(int j) throws Exception {
		double edgeCheckprob;

		for (int tempColumn = 2; tempColumn < eEdgesTable.getColumnCount(); tempColumn++) {
			edgeCheckprob = Double.parseDouble(((String) eEdgesTable
					.getValueAt(j, tempColumn)).trim());
			if (edgeCheckprob < 0.0 || edgeCheckprob > 1.0) {
				throw new Exception();
			}
		}

	}

	private void eTableNotCompete(int j) throws Exception {
		for (int tempColumn = 0; tempColumn < eEdgesTable.getColumnCount(); tempColumn++) {
			if (eTableNotNull(j) == false
					&& (eEdgesTable.getValueAt(j, tempColumn) instanceof String)
					&& (((String) eEdgesTable.getValueAt(j, tempColumn))
							.length() != 0)) {
				throw new Exception();
			}
		}

	}

	private boolean eTableNotNull(int j) {
		for (int tempColumn = 0; tempColumn < eEdgesTable.getColumnCount(); tempColumn++) {
			if ((eEdgesTable.getValueAt(j, tempColumn) instanceof String)
					&& (((String) eEdgesTable.getValueAt(j, tempColumn))
							.length() != 0)) {

			} else {
				// break;
				return false;
			}
		}
		return true;
	}

	private int checkLayers() throws GuiException {
		int numOfLayers = 0;
		try {
			numOfLayers = Integer.parseInt(layersTextbox.getText().trim());
		} catch (NumberFormatException e) {
			throw new GuiException("* Tree Layers needs to be integer",
					layerErrMsg);
		}
		if (numOfLayers < 1) {
			throw new GuiException(
					"* Tree Layers needs to be at least 1 or higher",
					layerErrMsg);
		} else if (numOfLayers == 1 && interfaceHandler.isStartByENode == false) {
			if (!((dEdgesTable.getValueAt(0, 0) instanceof String)
					&& (((String) dEdgesTable.getValueAt(0, 0)).length() != 0)
					&& (dEdgesTable.getValueAt(0, 1) instanceof String) && (((String) dEdgesTable
					.getValueAt(0, 1)).length() != 0))) {

				throw new GuiException(
						"* Decision Node branches needs to be at least has 1 edge",
						dTableErrMsg);

			}
		} else if (numOfLayers == 1 && interfaceHandler.isStartByENode == true
				&& eTableNotNull(0) == false) {
			throw new GuiException(
					"* Event Node branches needs to be at least has 1 edge",
					eTableTimeErrMsg);
		} else if (numOfLayers > 1
				&& (eTableNotNull(0) == false || (!((dEdgesTable.getValueAt(0,
						0) instanceof String)
						&& (((String) dEdgesTable.getValueAt(0, 0)).length() != 0)
						&& (dEdgesTable.getValueAt(0, 1) instanceof String) && (((String) dEdgesTable
						.getValueAt(0, 1)).length() != 0))))) {
			throw new GuiException(
					"* Node branches needs to be at least has 1 edge",
					layerErrMsg);
		}
		return numOfLayers;

	}

	private double checkTimes(JTextField textBox,JLabel label) throws GuiException {
		double time;
		try {
			time = Double.parseDouble(textBox.getText().trim());
		} catch (NumberFormatException e) {
			throw new GuiException("* Time needs to be decimal",
					label);
		}
		if (time < 0.0) {
			throw new GuiException("* Time needs to be at least 0.0 or higher",label);
		}
		return time;

	}
	
	private void checkDecisionTable() throws GuiException{
		for (int i = 0; i < dEdgesTable.getRowCount(); i++) {
			if (((dEdgesTable.getValueAt(i, 0) instanceof String) && ((String) dEdgesTable
					.getValueAt(i, 0)).length() != 0)
					&& (dEdgesTable.getValueAt(i, 1) instanceof String)
					&& ((String) dEdgesTable.getValueAt(i, 1)).length() != 0) {
				String EdgeName = ((String) dEdgesTable.getValueAt(i, 0))
						.trim();
				double curValue;
				try {
					curValue = Double.parseDouble(((String) dEdgesTable
							.getValueAt(i, 1)).trim());
				} catch (Exception e) {
					throw new GuiException("* Invalid value in row "
							+ (i + 1) + " needs to be decimal",dTableErrMsg);			
				}
				DecisionEdgeInfo DNodeInfo = new DecisionEdgeInfo(EdgeName,
						curValue);
				interfaceHandler.BasicEdgeList.add(DNodeInfo);
			} else if ((dEdgesTable.getValueAt(i, 0) == null && dEdgesTable
					.getValueAt(i, 1) != null)
					|| (dEdgesTable.getValueAt(i, 0) != null && dEdgesTable
							.getValueAt(i, 1) == null)) {
				dTableErrMsg.setText("* Incomplete information in row "
						+ (i + 1));
				return;
			} else if (((dEdgesTable.getValueAt(i, 0) instanceof String)
					&& ((String) dEdgesTable.getValueAt(i, 0)).length() == 0
					&& (dEdgesTable.getValueAt(i, 1) instanceof String) && ((String) dEdgesTable
					.getValueAt(i, 1)).length() != 0)
					|| (dEdgesTable.getValueAt(i, 0) instanceof String)
					&& ((String) dEdgesTable.getValueAt(i, 0)).length() != 0
					&& (dEdgesTable.getValueAt(i, 1) instanceof String)
					&& ((String) dEdgesTable.getValueAt(i, 1)).length() == 0) {
				dTableErrMsg.setText("* Incomplete information in row "
						+ (i + 1));
				return;
			}
		}

	}
	
	private void checkEventTable() throws GuiException{
		for (int j = 0; j < eEdgesTable.getRowCount(); j++) {
			if (eTableNotNull(j) == true) {
				String EdgeName = (String) eEdgesTable.getValueAt(j, 0);
				double curValue;
				try {
					curValue = Double.parseDouble(((String) eEdgesTable
							.getValueAt(j, 1)).trim());
				} catch (Exception e) {
					throw new GuiException("* Invalid value in row "
							+ (j + 1) + " needs to be decimal",eTableTimeErrMsg);				
				}
				try {
					eTableCheckprob(j);
				} catch (NumberFormatException e) {
					throw new GuiException("* Invalid probability in row "
							+ (j + 1) + " needs to be decimal",eTableTimeErrMsg);
				} catch (Exception e) {
					throw new GuiException("* Invalid probability in row "
							+ (j + 1)+ " needs to be at between 0.0 and 1.0",eTableTimeErrMsg);
				}
				double edgeprob = Double.parseDouble(((String) eEdgesTable
						.getValueAt(j, 2)).trim());
				ChanceEdgeInfo CNodeInfo = new ChanceEdgeInfo(EdgeName,
						curValue, edgeprob);
				interfaceHandler.ChanceEdgeList.add(CNodeInfo);
			}
			try {
				eTableNotCompete(j);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				throw new GuiException("* Incomplete information in row "
						+ (j + 1),eTableTimeErrMsg);				
			}
		}

	}
	
	private String checkText(JTextField text) throws Exception {

		if (NameTextbox.getText().trim().equals("")) {
			throw new GuiException("* Tree Name can not be NULL",treeNameErrMsg);
		}
		return text.getText().trim();
	}

	private double checkDoubleCellValue(String cellvalue) throws Exception {
		double doubleCellValue = Double.parseDouble(cellvalue.trim());
		return doubleCellValue;
	}

	private class AutoFillListener implements ActionListener {

		public void actionPerformed(ActionEvent event) {
			if (eEdgesTable.isEditing() && eEdgesTable.getCellEditor() != null) {
				eEdgesTable.getCellEditor().stopCellEditing();
			}
			int choose;
			for (int j = 0; j < eEdgesTable.getRowCount(); j++) {
				if (eTableNotNull(j) == true) {
					if (j == (eEdgesTable.getRowCount() - 1)) {
						DefaultTableModel dtm = (DefaultTableModel) eEdgesTable
								.getModel();
						dtm.addRow(new Object[][] {});
						eEdgesTable.updateUI();
						try {
							FillProb(j + 1);
						} catch (NumberFormatException e) {
							eTableTimeErrMsg
									.setText("The probability needs to be decimal");
							return;
						} catch (CannotProceedException e) {
							// TODO Auto-generated catch block
							eTableTimeErrMsg.setText(e.getMessage());
							return;
						}
					}
				} else {
					try {
						eTableNotCompete(j);
						FillProb(j);
						break;
					} catch (CannotProceedException e) {
						eTableTimeErrMsg.setText(e.getMessage());
						return;
					} catch (NumberFormatException e) {
						eTableTimeErrMsg
								.setText("The probability needs to be decimal");
						return;
					} catch (Exception e) {
						// TODO Auto-generated catch block
						choose = JOptionPane
								.showConfirmDialog(
										null,
										"Line "
												+ (j + 1)
												+ " is incomplete "
												+ "Do you want to auto fill remaining probability?",
										"remaining probability:",
										JOptionPane.OK_OPTION);
						if (choose == JOptionPane.OK_OPTION) {
							try {
								FillProb(j);
							} catch (Exception e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							}
							break;
						}
					}

				}
			}

		}
	}

	private void FillProb(int j) throws CannotProceedException {
		double sumProb = 0.0;
		for (int col = 2; col < eEdgesTable.getColumnCount(); col++) {
			sumProb = 0.0;
			for (int i = 0; i < j; i++) {
				double tempProb = Double.parseDouble(((String) eEdgesTable
						.getValueAt(i, col)).trim());
				sumProb = sumProb + tempProb;
			}
			if (sumProb > 1.0 || sumProb < 0.0) {
				throw new CannotProceedException(
						"The total of Probability in column " + (col + 1)
								+ " is " + sumProb
								+ " Needs to be less or equal 1.0");
			} else {
				String prob = (1 - sumProb)+"";
				eEdgesTable.setValueAt(prob, j, col);
			}
		}
	}

	/**
	 * The listener for the press of the submit button. When it happens, obtain
	 * the data values from the fields, and go back to LoadVensim model
	 * interface.
	 */

	private class BackListener implements ActionListener {
		public void actionPerformed(ActionEvent event) {
			setVisible(false);
			loadVensimUI.setVisible(true);

		}
	}

	public void updateChanceList(int layerOfChanceNode) {
		interfaceHandler.ChanceEdgeList.clear();
		layerOfChanceNode++;
		for (int j = 0; j < eEdgesTable.getRowCount(); j++) {
			if (eEdgesTable.getValueAt(j, 0) != null) {
				String EdgeName = (String) eEdgesTable.getValueAt(j, 0);
				double curValue = Double.parseDouble((String) eEdgesTable
						.getValueAt(j, 1));
				double edgeprob = Double.parseDouble((String) eEdgesTable
						.getValueAt(j, layerOfChanceNode));
				ChanceEdgeInfo CNodeInfo = new ChanceEdgeInfo(EdgeName,
						curValue, edgeprob);
				interfaceHandler.ChanceEdgeList.add(CNodeInfo);
			}
		}

	}

	private void paste() {
		Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
		DataFlavor flavor = DataFlavor.stringFlavor;
		if (clipboard.isDataFlavorAvailable(flavor)) {
			// try{
			try {
				String text = (String) clipboard.getData(flavor);
				String[] strArr = null;
				String intStr = "";
				for (int i = 0; i < text.length(); i++) {
					if (Character.isDigit(text.charAt(i))
							|| text.charAt(i) == '.') {
						intStr += text.charAt(i);
					} else {
						intStr += ",";
					}
				}
				strArr = intStr.split(",");
				int tempcolumn = eEdgesTable.getSelectedColumn();

				for (String intege : strArr) {
					eEdgesTable.setValueAt(intege,
							eEdgesTable.getSelectedRow(), tempcolumn++);
				}

			} catch (UnsupportedFlavorException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (RuntimeException e) {
				JOptionPane.showMessageDialog(null,
						"The numbers of probability is more than cells.");
				return;
			}
		}
	}

	public void drawColumn() {

		int numberLayers;
		try {
			numberLayers = Integer.parseInt(layersTextbox.getText().trim());
		} catch (NumberFormatException e) {
			layerErrMsg.setText("* Tree Layers needs to be integer");
			return;
		} catch (Exception e) {
			layerErrMsg.setText(e.getMessage());
			return;
		}

		DefaultTableModel dtm = (DefaultTableModel) eEdgesTable.getModel();
		dtm.setColumnCount(2);
		if (interfaceHandler.isStartByENode == false && numberLayers == 1) {
			dtm.addColumn("Probability");
		} else if (interfaceHandler.isStartByENode == false
				&& numberLayers % 2 == 0) {
			for (int i = 0; i < numberLayers / 2; i++) {
				dtm.addColumn("Probability" + (i + 1));
			}
		} else if (interfaceHandler.isStartByENode == false
				&& numberLayers % 2 == 1) {
			for (int i = 0; i < (numberLayers - 1) / 2; i++) {
				dtm.addColumn("Probability" + (i + 1));
			}
		} else if (interfaceHandler.isStartByENode == true && numberLayers == 1) {
			dtm.addColumn("Probability");
		} else if (interfaceHandler.isStartByENode == true
				&& numberLayers % 2 == 0) {
			for (int i = 0; i < numberLayers / 2; i++) {
				dtm.addColumn("Probability" + (i + 1));
			}
		} else {
			for (int i = 0; i < (numberLayers + 1) / 2; i++) {
				dtm.addColumn("Probability" + (i + 1));
			}
		}
		if (numberLayers > 2) {
			eEdgesTable.setAutoResizeMode(0);
		}
		dEdgesTable.updateUI();
	}

	public class LayerListener implements DocumentListener {
// @Override
		public void changedUpdate(DocumentEvent e) {
			// TODO Auto-generated method stub
		}
// @Override
		public void insertUpdate(DocumentEvent e) {
			// TODO Auto-generated method stub
			drawColumn();
		}
// @Override
		public void removeUpdate(DocumentEvent e) {
			// TODO Auto-generated method stub
		}
	}

	public class AddRow implements ActionListener {
		private JTable table;

		public AddRow(JTable table) {
			this.table = table;
		}
// @Override
		public void actionPerformed(ActionEvent event) {
			DefaultTableModel dtm = (DefaultTableModel) table.getModel();
			dtm.addRow(new Object[][] {});
			table.updateUI();

		}

	}

	public class DeleteRow implements ActionListener {
		private JTable table;

		public DeleteRow(JTable table) {
			this.table = table;
		}
//		@Override
		public void actionPerformed(ActionEvent event) {
			DefaultTableModel dtm = (DefaultTableModel) table.getModel();
			int numrow = table.getSelectedRows().length;
			for (int i = 0; i < numrow; i++) {
				dtm.removeRow(table.getSelectedRow());
			}
			table.updateUI();
		}
	}

	public class ProbListener extends MouseAdapter {
		// TODO Auto-generated method stub
		public void mouseClicked(MouseEvent e) {
			if (e.getClickCount() == 1 && eEdgesTable.getSelectedColumn() == 2) {
				int cont = JOptionPane.showConfirmDialog(null,
						"Do you want to use same probability?", "comment:",
						JOptionPane.OK_OPTION);
				if (cont != JOptionPane.OK_OPTION) {
					eEdgesTable.addKeyListener(new KeyListener() {
						 @SuppressWarnings("static-access")
//						 @Override
						public void keyPressed(KeyEvent e1) {
							// TODO Auto-generated method stub
							if (e1.isControlDown()
									&& e1.VK_V == e1.getKeyCode()
									&& eEdgesTable.getSelectedColumn() > 1) {
								paste();
							}
						}
// @Override
						public void keyReleased(KeyEvent arg0) {
							// TODO Auto-generated method stub
						}
 //@Override
						public void keyTyped(KeyEvent arg0) {
							// TODO Auto-generated method stub
						}

					});
				} else {
					eEdgesTable.addKeyListener(new KeyListener() {
 @SuppressWarnings("static-access")
// @Override
						public void keyPressed(KeyEvent e1) {
							// TODO Auto-generated method stub
							int tempcolumn = eEdgesTable.getSelectedColumn();
							if (e1.getKeyChar() == e1.VK_ENTER) {

								if (eEdgesTable.isEditing()
										&& eEdgesTable.getCellEditor() != null) {
									eEdgesTable.getCellEditor()
											.stopCellEditing();
								}
								String tempstring = (String) eEdgesTable
										.getValueAt(eEdgesTable
												.getSelectedRow(), tempcolumn);
								for (int i = 3; i < eEdgesTable
										.getColumnCount(); i++) {
									eEdgesTable.setValueAt(tempstring,
											eEdgesTable.getSelectedRow(), i);
								}
							}
						}
// @Override
						public void keyReleased(KeyEvent arg0) {
							// TODO Auto-generated method stub
						}
 //@Override
						public void keyTyped(KeyEvent arg0) {
							// TODO Auto-generated method stub
						}

					});
				}

			}
		}

	}

	public static final long serialVersionUID = 1;

}
